import VXETable from 'vxe-table'
import Cookies from 'js-cookie'
import XEUtils from 'xe-utils'
import VXETablePluginExportXLSX from 'vxe-table-plugin-export-xlsx'
import VXETablePluginExportPDF from 'vxe-table-plugin-export-pdf'

VXETable.use(VXETablePluginExportXLSX)

// 支持中文字体
VXETablePluginExportPDF.setup({
  fontName: 'SourceHanSans-Normal',
  fonts: [
    {
      // Font name
      fontName: 'SourceHanSans-Normal',
      // Font library url
      fontUrl: 'https://cdn.jsdelivr.net/npm/vxe-table-plugin-export-pdf/fonts/source-han-sans-normal.js'
    }
  ]
})

VXETable.use(VXETablePluginExportPDF)

VXETable.formats.add('money', ({ cellValue }, digits = 2) => {
  if (cellValue == null) {
    return ''
  }
  return XEUtils.commafy(XEUtils.toNumber(cellValue), { digits })
})

VXETable.formats.add('switch', ({ cellValue }) => {
  if (cellValue === true) {
    return '<span style="color: #409eff">是</span>'
  }
  return '<span class="column-primary">否</span>'
})

VXETable.config({
  size: Cookies.get('size') || 'small', // 全局尺寸
  // zIndex: 999, // 全局 zIndex 起始值，如果项目的的 z-index 样式值过大时就需要跟随设置更大，避免被遮挡
  version: 0, // 版本号，对于某些带数据缓存的功能有用到，上升版本号可以用于重置数据
  loadingText: '加载中...', // 全局loading提示内容，如果为null则不显示文本
  table: {
    showHeader: true,
    keepSource: false,
    showOverflow: null,
    showHeaderOverflow: null,
    showFooterOverflow: null,
    size: null,
    autoResize: true,
    stripe: true,
    border: true,
    round: false,
    emptyText: '暂无数据',
    resizeConfig: {
      refreshDelay: 250
    },
    rowConfig: {
      keyField: 'id', // 行数据的唯一主键字段名,
      isCurrent: true,
      isHover: true
    },
    columnConfig: {
      resizable: true
    },
    radioConfig: {
      trigger: 'default'
    },
    checkboxConfig: {
      strict: false,
      highlight: false,
      range: false,
      trigger: 'default'
    },
    sortConfig: {
      remote: true,
      trigger: 'default',
      orders: ['asc', 'desc'],
      sortMethod: null
    },
    filterConfig: {
      remote: false,
      filterMethod: null
    },
    expandConfig: {
      trigger: 'default',
      showIcon: true
    },
    treeConfig: {
      rowField: 'id',
      parentField: 'parentId',
      childrenField: 'children',
      hasChild: 'hasChild',
      mapChildren: '_X_ROW_CHILD',
      indent: 20,
      showIcon: true
    },
    tooltipConfig: {
      enterable: true
    },
    menuConfig: {
      visibleMethod() {}
    },
    editConfig: {
      mode: 'cell',
      showAsterisk: true
    },
    importConfig: {
      modes: ['insert']
    },
    exportConfig: {
      // 默认选中类型
      type: 'xlsx',
      // 局部自定义类型
      types: ['xlsx', 'csv', 'html', 'xml', 'txt', 'pdf'],
      // 自定义数据量列表
      modes: ['current', 'selected']
    },
    customConfig: {
      storage: false
    },
    'area-config': {
      multiple: false
    },
    scrollX: {
      gt: 60
    },
    scrollY: {
      gt: 100
    },
    loading: {
      icon: 'vxe-icon-spinner roll',
      text: '加载中...'
    }
  },
  grid: {
    size: null,
    zoomConfig: {
      escRestore: true
    },
    pagerConfig: {
      perfect: false
    },
    toolbarConfig: {
      perfect: false
    },
    proxyConfig: {
      autoLoad: true,
      message: true,
      form: true,
      sort: true,
      props: {
        list: 'data', // 用于列表，读取响应数据
        result: 'data', // 用于分页，读取响应数据
        total: 'rows' // 用于分页，读取总条数
      },
      beforeItem: null,
      beforeColumn: null,
      beforeQuery: null,
      afterQuery: null,
      beforeDelete: null,
      afterDelete: null,
      beforeSave: null,
      afterSave: null
    }
  },
  pager: {
    background: true,
    size: null,
    autoHidden: false,
    perfect: true,
    pageSize: 10,
    pagerCount: 7,
    pageSizes: [10, 20, 100, { label: '大量数据', value: 1000 }, { label: '全量数据', value: -1 }],
    layouts: ['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']
  },
  form: {
    preventSubmit: false,
    size: null,
    colon: false,
    validConfig: {
      autoPos: true
    },
    tooltipConfig: {
      enterable: true
    },
    titleAsterisk: true
  },
  input: {
    size: null,
    transfer: false,
    parseFormat: 'yyyy-MM-dd HH:mm:ss.SSS',
    labelFormat: '',
    valueFormat: '',
    startDay: 1,
    digits: 2,
    controls: true
  },
  textarea: {
    size: null,
    autosize: {
      minRows: 1,
      maxRows: 10
    }
  },
  select: {
    size: null,
    transfer: false,
    optionConfig: {
      keyField: '_X_OPTION_KEY' // 选项数据的唯一主键字段名
    },
    multiCharOverflow: 8
  },
  toolbar: {
    size: null,
    import: {
      mode: 'insert'
    },
    export: {
      types: ['csv', 'html', 'xml', 'txt', 'xlsx']
    },
    custom: {
      // isFooter: true
    },
    buttons: [],
    tools: []
  },
  button: {
    size: null,
    transfer: false
  },
  radio: {
    size: null
  },
  checkbox: {
    size: null
  },
  switch: {
    size: null
  },
  modal: {
    // size: null,
    minWidth: 500,
    minHeight: 200,
    lockView: true,
    mask: true,
    duration: 3000,
    marginSize: 0,
    dblclickZoom: true,
    showTitleOverflow: true,
    storage: false
  },
  list: {
    scrollY: {
      gt: 100
    }
  }
})
